package com.riddle.model;

import java.util.ArrayList;
import java.util.List;

import com.riddle.util.StringUtil;

public class Role implements TreeModel<Role>  {

	private Long id;
	
	private String name;
	
	private String alias;
	
	private String description;
	
	private boolean available;
	
	private Role parent;
	
	private List<Role> children = new ArrayList<Role>();

	private List<Permission> permissions = new ArrayList<Permission>();
	
	private String permissionsStr = null;
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAlias() {
		return alias;
	}

	public void setAlias(String alias) {
		this.alias = alias;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public boolean isAvailable() {
		return available;
	}

	public void setAvailable(boolean available) {
		this.available = available;
	}

	public Role getParent() {
		return parent;
	}

	public void setParent(Role parent) {
		this.parent = parent;
	}

	public List<Role> getChildren() {
		return children;
	}

	public void setChildren(List<Role> children) {
		this.children = children;
	}

	public List<Permission> getPermissions() {
		return permissions;
	}

	public void setPermissions(List<Permission> permissions) {
		this.permissions = permissions;
	}

	public String getPermissionsStr() {
		if(StringUtil.isNullOrEmpty(permissionsStr)) {
			if(getPermissions() != null && getPermissions().size() > 0) {
				StringBuffer sb = new StringBuffer();
				for(Permission p : getPermissions()) {
					if(p != null) {
						sb.append(p.getId());
						sb.append(",");
					}
				}
				if(sb.length() > 0) {
					permissionsStr = sb.toString().substring(0, sb.length() - 1);
				}
			}
		}
		return permissionsStr;
	}

	public void setPermissionsStr(String permissionsStr) {
		this.permissionsStr = permissionsStr;
	}
}
